package com.sgcc.pda.hzaidl.impl;

import android.os.RemoteException;

import com.sgcc.pda.hw.ISealCallback;
import com.sgcc.pda.hw.ReturnResult;
import com.sgcc.pda.hzaidl.entity.HardWareCallback;
import com.sgcc.platforminterface.PlatfInterfManager;


import org.json.JSONException;
import org.json.JSONObject;


/**
 * @创建者：suven on 2019/12/17 21:48
 * @描述：电子封印
 * @版本：1.0
 * @修改人：
 * @修改时间：
 * @修改说明：
 * @修改版本：
 */
public class SealManager {
    private static SealManager mInstance;

    /**
     * 单例
     *
     * @return 引用
     */
    public static synchronized SealManager getInstance() {
        if (mInstance == null) {
            mInstance = new SealManager();
        }
        return mInstance;
    }

    /**
     * 读取指定地址数据
     * sealKid 密钥Kid A区:01  B区:02
     * addr A区:8-32  B区:48-64 多条;隔开
     */
    public ReturnResult readByAddr(String sealKid, String addr) throws RemoteException {
        if (PlatfInterfManager.getInstance().isSpeed()) {
            JSONObject jb = new JSONObject();
            try {
                jb.put("sealKid", sealKid);
                jb.put("addr", addr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return HardWareUtil.requestHardwareData("seal_readByAddr", jb.toString());
        } else
            return com.sgcc.pda.hw.impl.SealManager.getInstance().getSealManager().readByAddr(sealKid, addr);
    }

    /**
     * 读取
     * sealKid 密钥Kid A区:01  B区:02
     */
    public ReturnResult read(String sealKid) throws RemoteException {
        if (PlatfInterfManager.getInstance().isSpeed()) {
            JSONObject jb = new JSONObject();
            try {
                jb.put("sealKid", sealKid);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return HardWareUtil.requestHardwareData("seal_read", jb.toString());
        } else
            return com.sgcc.pda.hw.impl.SealManager.getInstance().getSealManager().read(sealKid);
    }

    /**
     * 写入
     * sealKeyType   00为公钥权限   01 为私钥权限
     * sealNo        封印序列号 读取标签地址为8的后16位
     * data          要写入的数据 写入的数据 多条;隔开
     * sealDataAdd   数据地址 写入地址 多条;隔开 对应data
     */
    public void write(String sealKeyType, String sealNo, String data,
                      String sealDataAdd, final HardWareCallback hardWareCallback) throws RemoteException {
        if (PlatfInterfManager.getInstance().isSpeed()) {
            JSONObject jb = new JSONObject();
            try {
                jb.put("sealKeyType", sealKeyType);
                jb.put("sealNo", sealNo);
                jb.put("data", data);
                jb.put("sealDataAdd", sealDataAdd);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            HardWareUtil.requestHardwareData("seal_write", jb.toString(), hardWareCallback);
        } else {
            com.sgcc.pda.hw.impl.SealManager.getInstance().getSealManager().write(sealKeyType, sealNo, data,
                    sealDataAdd, new ISealCallback.Stub() {
                        @Override
                        public void onCallback(ReturnResult returnResult) throws RemoteException {
                            if (hardWareCallback != null)
                                hardWareCallback.onReceiverData(returnResult);
                        }
                    });
        }
    }

    /**
     * 更新秘钥权限
     * updateType    秘钥更新类型 01为发行   00 为清除
     * sealNo        封印序列号 读取标签地址为8的后16位
     */
    public void update(String updateType, String sealNo, final HardWareCallback hardWareCallback) throws RemoteException {
        if (PlatfInterfManager.getInstance().isSpeed()) {
            JSONObject jb = new JSONObject();
            try {
                jb.put("updateType", updateType);
                jb.put("sealNo", sealNo);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            HardWareUtil.requestHardwareData("seal_update", jb.toString(), hardWareCallback);
        } else {
            com.sgcc.pda.hw.impl.SealManager.getInstance().getSealManager().update(updateType, sealNo, new ISealCallback.Stub() {
                @Override
                public void onCallback(ReturnResult returnResult) throws RemoteException {
                    if (hardWareCallback != null)
                        hardWareCallback.onReceiverData(returnResult);
                }
            });
        }
    }


}
